<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;

class ShopHomeModel extends Model
{
     //定义一个表名
    protected $table = 'shop_home';
    //定义主键
    protected $primaryKey = 'id';

	//启用禁用
	public function updateState($id,$state)
	{
		$res = $this->find($id);
		if($state == 1){
			$msg = '更改首页入口【'.$res['name'].'】(id:'.$res['id'].')状态为【开启】';
		}
		if($state == 2){
			$msg = '更改首页入口【'.$res['name'].'】(id:'.$res['id'].')状态为【关闭】';
		}

		$arr = $this->where("id",$id)->update(['state' => $state]);
		if($arr){
			//添加日志
			AdminLogsModel::addLog($msg);
			$returnArr['code'] = 1;
			$returnArr['msg'] = "修改成功";
		}else{
			$returnArr['code'] = 2;
			$returnArr['msg'] = "修改失败";
		}
		return $returnArr;
	}

	//查询类型列表
	static function doList(){
		$data = [];
		$list = ShopHomeModel::limit(4)->orderBy('sort','asc')->get();
		foreach($list as $k=>$v){
			$data[$k]['id'] = $v['id'];
			$data[$k]['name'] = $v['name'];
			$v['picname'] = "http://".$_SERVER['HTTP_HOST'].$v['picpath'].$v['picname'];
		}
		return $data;
	}
}